package b1.mobile.blackberry.ui.component;

import java.util.Vector;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Ui;
import b1.mobile.blackberry.ui.component.util.FontFactory;
import b1.mobile.blackberry.util.StringUtil;

public class ListGroupItemField extends Field {
	private static Font systemFont = FontFactory.getFont(Font.PLAIN, 6,
			Ui.UNITS_pt);

	// public static int TYPE_HEADERINFO = 1;
	// public static int TYPE_SINGLE_TEXT = 2;
	// public static int TYPE_NORMAL = 3;
	public static int TYPE_LABEL = 4;

	public static int DRAWPOSITION_TOP = 0;
	public static int DRAWPOSITION_BOTTOM = 1;
	public static int DRAWPOSITION_MIDDLE = 2;
	public static int DRAWPOSITION_SINGLE = 3;

	private static final int CORNER_RADIUS = 1;

	private static final int HPADDING = Display.getWidth() <= 320 ? 6 : 8;
	private static final int VPADDING = 4;

	private static final int COLOR_BACKGROUND = 0xF3F3F3;
	private static final int COLOR_BORDER = 0xBBBBBB;
	private static final int COLOR_BACKGROUND_FOCUS = 0x186DEF;

	private Bitmap leftIcon;
	private Bitmap actionIcon;

	private int _drawPosition = -1;

	private String key;
	public String content;
	private Vector lines;

	private String singleText;

	private InfoHeader infoHeader;

	private int type;

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public ListGroupItemField(Bitmap icon, String key, String content,
			Bitmap actionIcon, long style) {
		super(USE_ALL_WIDTH | Field.FOCUSABLE);

		this.actionIcon = actionIcon;
		leftIcon = icon;

		this.key = key;
		this.content = content;
	}

	public ListGroupItemField(String key, String content, boolean arrow) {
		super(USE_ALL_WIDTH | Field.FOCUSABLE);

		if (arrow) {
			this.actionIcon = Bitmap.getBitmapResource("arrow_grey_R.png");
		}
		leftIcon = null;

		this.key = key;
		this.content = content;
	}

	public ListGroupItemField(String singleText) {
		super(USE_ALL_WIDTH | Field.FOCUSABLE);

		this.actionIcon = Bitmap.getBitmapResource("arrow_grey_R.png");

		leftIcon = null;

		this.key = null;
		this.content = null;
		this.singleText = singleText;
	}

	public ListGroupItemField(String singleText, long style) {
		super(USE_ALL_WIDTH | style);

		this.actionIcon = null;

		leftIcon = null;

		this.key = null;
		this.content = null;
		this.singleText = singleText;
	}

	public ListGroupItemField(InfoHeader infoHeader) {
		super(USE_ALL_WIDTH);

		actionIcon = null;

		leftIcon = null;

		this.key = null;
		this.content = null;
		this.singleText = null;
		this.infoHeader = infoHeader;

	}

	public ListGroupItemField(String key, String content, Bitmap buttonIcon) {
		super(USE_ALL_WIDTH | Field.FOCUSABLE);
		this.actionIcon = null;

		leftIcon = buttonIcon;

		this.key = key;
		this.content = content;
	}

	public void setDrawPosition(int drawPosition) {
		_drawPosition = drawPosition;
	}

	public void layout(int width, int height) {
		if (type == TYPE_LABEL) {
			setExtent(width, systemFont.getHeight());
		} else if (singleText == null && infoHeader == null) {
			lines = StringUtil.parseMessage(content, systemFont, width * 2 / 3 - 40);
			int size = Math.max(1, lines.size());
			int myHeight = size * systemFont.getHeight() + 20;
			setExtent(width, myHeight);
		} else if (singleText != null && infoHeader == null) {
			setExtent(width, systemFont.getHeight() * 2);
		} else {
			int maxHeight = 0;

			if (infoHeader.getIcon().getHeight() >= systemFont.getHeight() * 4) {
				maxHeight = infoHeader.getIcon().getHeight();
			} else {
				maxHeight = systemFont.getHeight() * 4;
			}

			setExtent(Display.getWidth(), maxHeight);
		}
	}

	public void setText(String text) {
		updateLayout();
	}

	protected void paint(Graphics g) {
		if (leftIcon != null) {
			g.drawBitmap(HPADDING, VPADDING, leftIcon.getWidth(),
					leftIcon.getHeight(), leftIcon, 0, 0);
		}

		g.setFont(systemFont);
		if (leftIcon == null) {
			if (this.type == TYPE_LABEL) {
				g.setColor(Color.GRAY);
				g.fillRect(0, 0, getWidth(), getHeight());
				g.setColor(0x00333333);
				g.drawText(singleText, 3,
						(getHeight() - systemFont.getHeight()) / 2);

			} else if (this.singleText == null && this.infoHeader == null) {
				int lineSize = lines.size();
				int startY = (getHeight() - (systemFont.getHeight() * Math.max(1, lineSize))) / 2;
				
				g.setColor(Color.GRAY);
				int keyOffset = getWidth() / 3 - systemFont.getAdvance(key);
				g.drawText(key, keyOffset, startY);
				g.setColor(Color.BLACK);

				for (int i = 0; i < lineSize; i++) {
					g.drawText((String) lines.elementAt(i),
							getWidth() / 3 + 20,
							startY + i * systemFont.getHeight());
				}
			} else if (this.singleText != null) {
				g.setColor(Color.BLACK);
				int startY = (getHeight() - systemFont.getHeight()) / 2;
				int keyOffset = (getWidth() - systemFont.getAdvance(singleText)) / 2;
				g.drawText(singleText, keyOffset, startY);
			} else if (this.infoHeader != null) {
				g.drawBitmap(systemFont.getAdvance("W"),
						(getHeight() - infoHeader.getIcon().getHeight()) / 2,
						infoHeader.getIcon().getWidth(), infoHeader.getIcon()
								.getHeight(), infoHeader.getIcon(), 0, 0);

				int textXOffset = infoHeader.getIcon().getWidth()
						+ systemFont.getAdvance("W") * 3;

				int textYOffset = (getHeight() - systemFont.getHeight() * 3) / 2;

				// name
				g.drawText(infoHeader.getName(), textXOffset, textYOffset);

				g.drawText(infoHeader.getCode(), textXOffset, textYOffset
						+ systemFont.getHeight() + 3);

				g.drawText(infoHeader.getType(), textXOffset, textYOffset
						+ systemFont.getHeight() * 2 + 3);
			}
		} else {
			g.drawText(content,
					(getWidth() - systemFont.getAdvance(content)) / 2,
					(getHeight() - systemFont.getHeight()) / 2);
		}

		if (actionIcon != null) {
			g.drawBitmap(getWidth() - HPADDING - actionIcon.getWidth() - 5,
					(getHeight() - actionIcon.getHeight()) / 2,
					actionIcon.getWidth(), actionIcon.getHeight(), actionIcon,
					0, 0);
		}
	}

	protected void paintBackground(Graphics g) {
		if (_drawPosition < 0) {
			super.paintBackground(g);
			return;
		}

		int oldColour = g.getColor();

		int background = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? COLOR_BACKGROUND_FOCUS
				: COLOR_BACKGROUND;
		try {
			if (_drawPosition == 0) {
				// Top
				g.setColor(background);
				g.fillRoundRect(0, 0, getWidth(), getHeight() + CORNER_RADIUS,
						CORNER_RADIUS, CORNER_RADIUS);
				g.setColor(COLOR_BORDER);
				g.drawRoundRect(0, 0, getWidth(), getHeight() + CORNER_RADIUS,
						CORNER_RADIUS, CORNER_RADIUS);
				g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
			} else if (_drawPosition == 1) {
				// Bottom
				g.setColor(background);
				g.fillRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight()
						+ CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
				g.setColor(COLOR_BORDER);
				g.drawRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight()
						+ CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
			} else if (_drawPosition == 2) {
				// Middle
				g.setColor(background);
				g.fillRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + 2
						* CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
				g.setColor(COLOR_BORDER);
				g.drawRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + 2
						* CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
				g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
			} else {
				// Single
				g.setColor(background);
				g.fillRoundRect(0, 0, getWidth(), getHeight(), CORNER_RADIUS,
						CORNER_RADIUS);
				g.setColor(COLOR_BORDER);
				g.drawRoundRect(0, 0, getWidth(), getHeight(), CORNER_RADIUS,
						CORNER_RADIUS);
			}
		} finally {
			g.setColor(oldColour);
		}
	}

	protected void drawFocus(Graphics g, boolean on) {
		if (_drawPosition < 0) {
			super.drawFocus(g, on);
		} else {
			boolean oldDrawStyleFocus = g
					.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS);
			try {
				if (on) {
					g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);
				}
				paintBackground(g);
				paint(g);
			} finally {
				g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus);
			}
		}
	}

	protected boolean keyChar(char character, int status, int time) {
		if (character == Characters.ENTER) {
			clickButton();
			return true;
		}
		return super.keyChar(character, status, time);
	}

	protected boolean navigationClick(int status, int time) {
		clickButton();
		return true;
	}

	protected boolean trackwheelClick(int status, int time) {
		clickButton();
		return true;
	}

	public void clickButton() {
		fieldChangeNotify(0);
	}

	public void setDirty(boolean dirty) {
	}

	public void setMuddy(boolean muddy) {
	}

}
